using B.S.Meeting.Infrastructure.Implements;
using B.S.Meeting.Infrastructure.Interfaces;
using B.S.Meeting.Infrastructure;
using B.S.Meeting.WriteAPI;
using Microsoft.EntityFrameworkCore;
using NLog.Web;
using System.Reflection;
using Yitter.IdGenerator;
using Lazy.Captcha.Core.Generator;
using Lazy.Captcha.Core;
using B.S.Meeting.ReadAPI.Filter;
using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.


// 在构建器中访问 Environment 属性
var environmentName = builder.Environment.EnvironmentName;

Console.WriteLine($"Current Environment: {environmentName}");
// 或者使用 logger，如果已经配置了日志服务
// builder.Services.AddLogging(); // 确保日志服务已添加
// var logger = builder.Services.BuildServiceProvider().GetRequiredService<ILogger<Program>>();
// logger.LogInformation($"Current Environment: {environmentName}");

builder.Services.AddDbContext<BaseDbContext>((serviceProvider, x) => 
{
    x.UseSqlServer(builder.Configuration.GetConnectionString("Str"));
    // 添加审计拦截器
    x.AddInterceptors(serviceProvider.GetRequiredService<AuditTrailInterceptor>());
});

builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));

// 默认使用内存存储（AddDistributedMemoryCache）
builder.Services.AddCaptcha(builder.Configuration);
// 全部配置
builder.Services.AddCaptcha(builder.Configuration, option =>
{
    option.CaptchaType = CaptchaType.WORD; // 验证码类型
    option.CodeLength = 6; // 验证码长度, 要放在CaptchaType设置后.  当类型为算术表达式时，长度代表操作的个数
    option.ExpirySeconds = 30; // 验证码过期时间
    option.IgnoreCase = true; // 比较时是否忽略大小写
    option.StoreageKeyPrefix = ""; // 存储键前缀

    option.ImageOption.Animation = true; // 是否启用动画
    option.ImageOption.FrameDelay = 30; // 每帧延迟,Animation=true时有效, 默认30

    option.ImageOption.Width = 150; // 验证码宽度
    option.ImageOption.Height = 50; // 验证码高度
    option.ImageOption.BackgroundColor = SkiaSharp.SKColors.White; // 验证码背景色

    option.ImageOption.BubbleCount = 2; // 气泡数量
    option.ImageOption.BubbleMinRadius = 5; // 气泡最小半径
    option.ImageOption.BubbleMaxRadius = 15; // 气泡最大半径
    option.ImageOption.BubbleThickness = 1; // 气泡边沿厚度

    option.ImageOption.InterferenceLineCount = 2; // 干扰线数量

    option.ImageOption.FontSize = 36; // 字体大小
    option.ImageOption.FontFamily = DefaultFontFamilys.Instance.Actionj; // 字体

    /* 
     * 中文使用kaiti，其他字符可根据喜好设置（可能部分转字符会出现绘制不出的情况）。
     * 当验证码类型为"ARITHMETIC"时，不要使用"Ransom"字体。（运算符和等号绘制不出来）
     */

    option.ImageOption.TextBold = true;// 粗体，该配置2.0.3新增
});

// 添加内存缓存
builder.Services.AddMemoryCache();

builder.Services.AddSingleton<AuditTrailInterceptor>();

// 注册验证码服务
builder.Services.AddScoped<IVerificationCodeService, VerificationCodeService>();

builder.Services.AddMediatR(x => x.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));

builder.Services.AddAutoMapper(typeof(MappingProfiles));

builder.Host.UseNLog();

YitIdHelper.SetIdGenerator(new IdGeneratorOptions());

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(x =>
{
    var file = Path.Combine(AppContext.BaseDirectory + "B.S.Meeting.WriteAPI.xml");
    x.IncludeXmlComments(file, true);
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseStaticFiles();
app.UseCors(x => x.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());

app.UseAuthorization();

app.MapControllers();

app.Run();
